﻿using BLL;
using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FangChanCrm.Controllers
{
    public class GongSiController : Controller
    {
        private readonly It_GongSiBiaoService it_GongSiBiaoService;
        private readonly It_BuMenBiaoService it_BuMenBiaoService;
        private readonly It_GangWeiBiaoService it_GangWeiBiaoService;
        private readonly It_ZhanQuBiaoService it_ZhanQuBiaoService;
        private readonly It_ZhanDuiBiaoService it_ZhanDuiBiaoService;

        public GongSiController(It_GongSiBiaoService it_GongSiBiaoService, It_BuMenBiaoService it_BuMenBiaoService, It_GangWeiBiaoService it_GangWeiBiaoService, It_ZhanQuBiaoService it_ZhanQuBiaoService, It_ZhanDuiBiaoService it_ZhanDuiBiaoService, IConfiguration configuration)
        {
            this.it_GongSiBiaoService = it_GongSiBiaoService;
            this.it_BuMenBiaoService = it_BuMenBiaoService;
            this.it_GangWeiBiaoService = it_GangWeiBiaoService;
            this.it_ZhanQuBiaoService = it_ZhanQuBiaoService;
            this.it_ZhanDuiBiaoService = it_ZhanDuiBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        //private readonly It_RenYuanBiaoService it_RenYuanBiaoService;
        
        public IActionResult GongSiAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }

            }
            else
            {
                return View();
            }
        }
        public IActionResult GongSiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }

            }
            else
            {
                return View();
            }
        }

        public IActionResult ZuZhiJiaGouTu()
        {
            //if (HttpContext.Session.GetString("loginid") == null)
            //{
            //    HttpContext.Request.Cookies.TryGetValue("账户", out string value);
            //    if (value == null || value == "")
            //    {
            //        return View("../Login/Login");
            //    }
            //    else
            //    {
            //        return View("../Login/LoginChongXin");
            //    }

            //}
            //else
            //{
            //    return View();
            //}
            return View();
        }


        #region 添加
        [HttpPost]
        public int Add()
        {
            t_GongSiBiao t_GongSiBiao = new t_GongSiBiao();
            t_GongSiBiao.GongSiMingCheng = Request.Form["ipt_GongSiMingCheng"];
            t_GongSiBiao.ChengLiShiJian = Request.Form["ipt_ChengLiShiJian"];
            t_GongSiBiao.GongSiDiZhi = Request.Form["ipt_GongSiDiZhi"];
            t_GongSiBiao.LianXiFangShi = Request.Form["ipt_LianXiFangShi"];
            //t_GongSiBiao.FuZeRen = Convert.ToInt32(Request.Form["slt_FuZeRen"]);
            t_GongSiBiao.FaRen = Request.Form["slt_FaRen"];
            t_GongSiBiao.GongSiGuiMo = Request.Form["ipt_GongSiGuiMo"];
            t_GongSiBiao.GuanFangWangZhan = Request.Form["ipt_GuanFangWangZhan"];
            t_GongSiBiao.GongSiJianJie = Request.Form["ipt_GongSiJianJie"];
            return it_GongSiBiaoService.Add(t_GongSiBiao).Id;
        }
        #endregion
        #region 列表
        [HttpPost]
        public string GetListWithFenYe()
        {

            t_GongSiBiao t_GongSiBiao = new t_GongSiBiao();
            t_GongSiBiao.Id = Convert.ToInt32(Request.Form["Id"]);
            t_GongSiBiao.GongSiMingCheng = Request.Form["ipt_GongSiMingCheng"];
            t_GongSiBiao.ChengLiShiJian = Request.Form["ipt_ChengLiShiJian"];
            t_GongSiBiao.GongSiDiZhi = Request.Form["ipt_GongSiDiZhi"];
            //t_GongSiBiao.FuZeRen = Convert.ToInt32(Request.Form["slt_FuZeRen"]);
            //t_GongSiBiao.FaRen = Convert.ToInt32(Request.Form["slt_FaRen"]);

            return it_GongSiBiaoService.GetListWithFenYe(t_GongSiBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        #endregion
        #region 修改
        public int Update()
        {
            int gongsiid = 0;
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var t_GongSi = _context.t_GongSiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                gongsiid = Convert.ToInt32(Request.Form["Id"]);
                t_GongSi.GongSiMingCheng = Request.Form["ipt_GongSiMingCheng"];
                t_GongSi.ChengLiShiJian = Request.Form["ipt_ChengLiShiJian"];
                t_GongSi.GongSiDiZhi = Request.Form["ipt_GongSiDiZhi"];
                t_GongSi.LianXiFangShi = Request.Form["ipt_LianXiFangShi"];
                t_GongSi.FaRen = Request.Form["slt_FaRen"];
                t_GongSi.GongSiGuiMo = Request.Form["ipt_GongSiGuiMo"];
                t_GongSi.GuanFangWangZhan = Request.Form["ipt_GuanFangWangZhan"];
                t_GongSi.GongSiJianJie = Request.Form["ipt_GongSiJianJie"];
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan == true)
            {
                return gongsiid;
            }
            else
            {
                return -1;
            }
        }
        #endregion
        #region 绑定
        public string GetById()
        {
            return JsonConvert.SerializeObject(new List<t_GongSiBiao> { it_GongSiBiaoService.GetById(Convert.ToInt32(Request.Form["Id"])) }, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 下拉框
        //[HttpPost]
        //public string GetSelRenYuan()
        //{
        //    return JsonConvert.SerializeObject(it_RenYuanBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        //}
        #endregion

        #region 拼接公司组织架构数据源
        [HttpPost]
        public string GetGongSiZuZhiJiaGou()
        {
            List<t_BuMenBiao> listbumen = it_BuMenBiaoService.GetList().Where(x => x.GongSiId == 8&&x.Id!=1).ToList();
            string json = "{\"data\":[{\"id\":1,\"name\":\"淘房侠有限公司\",\"pid\":null,\"childrens\":[";
            for (int i = 0; i < listbumen.Count; i++)
            {
                json += "{\"id\":"+listbumen[i].Id+",\"name\":\""+listbumen[i].BuMenMingCheng+"\",\"pid\":1,\"childrens\":[";
                if (listbumen[i].ZhanQuZhuangTai == "战区部门")
                {
                    List<t_ZhanQuBiao> listzhanqu = it_ZhanQuBiaoService.GetList().Where(x => x.BuMenId == listbumen[i].Id && x.Id != 1).ToList();

                    for (int n = 0; n < listzhanqu.Count; n++)
                    {
                        json += "{\"id\":"+listzhanqu[n].Id+",\"name\":\""+listzhanqu[n].ZhanQuMingCheng+"\",\"pid\":"+listbumen[i].Id+",\"childrens\":[";

                        List<t_ZhanDuiBiao> listzhandui = it_ZhanDuiBiaoService.GetList().Where(x => x.ZhanQuId == listzhanqu[n].Id && x.id != 1).ToList();
                        for (int j = 0; j < listzhandui.Count; j++)
                        {
                            json += "{\"id\":" + listzhandui[j].id + ",\"name\":\"" + listzhandui[j].ZhanDuiMingCheng + "\",\"pid\":" + listzhanqu[n].Id + ",\"childrens\":[]},";
                            if (j==listzhandui.Count-1)
                            {
                                json = json.Substring(0, json.Length - 1);
                            }
                        }

                        json += "]},";
                        if (n==listzhanqu.Count-1)
                        {
                            json = json.Substring(0, json.Length - 1);
                        }
                    }

                    json += "]";
                }
                else
                {
                    List<t_GangWeiBiao> listgangwei = it_GangWeiBiaoService.GetList().Where(x => x.BuMenId == Convert.ToInt32(listbumen[i].Id)&&x.Id!=1).ToList();
                    for (int m = 0; m < listgangwei.Count; m++)
                    {
                        json += "{\"id\":"+listgangwei[m].Id+",\"name\":\""+listgangwei[m].GangWeiMingCheng+"\",\"pid\":"+listbumen[i].Id+",\"childrens\":[]},";
                        if (m==listgangwei.Count-1)
                        {
                            json = json.Substring(0, json.Length - 1);
                        }
                    }
                    json += "]";
                }
                json += "},";
                if (i==listbumen.Count-1)
                {
                    json = json.Substring(0, json.Length - 1);
                }
            }
            json += "]}]}";

            return json;
        }
        #endregion
    }
}
